/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.ai;

import java.util.LinkedList;
import java.util.Random;
import rpg.Location;
import rpg.entities.Entity;
import rpg.entities.EntityHandler;
import rpg.entities.LivingEntity;
import rpg.util.AStar;

/**
 *
 * @author rothens
 */
public class AIFollowOwner extends AI {

    private AStar as;
    private LinkedList<Location> path;
    int nextCheck = 3;
    private LivingEntity owner;
    private boolean nearOwner = false;

    public AIFollowOwner(LivingEntity entity, int speed) {
        super(entity);
        as = new AStar();
        this.speed = speed;
    }

    @Override
    public boolean tick() {
        if (entity == null || entity.getHealth() <= 0) {
            return false;
        }

        if (owner != null) {
            double dist = entity.getCurrentLocation().getDistance(owner.getCurrentLocation());
            nearOwner = dist <= 180;
            if (!nearOwner && (path == null || path.isEmpty() || nextCheck <= 0)) {
                path = as.getPath(entity.getCurrentLocation(), owner.getCurrentLocation(), entity.getMap());
                nextCheck = 3;
            }
            if (entity.getCurrentLocation() == null) {
                return true;
            }
            if (!nearOwner) {
                nextLoc = path.getFirst().normalize(entity.getCurrentLocation());
                if (entity.getCurrentLocation().equals(path.getFirst(), speed)) {
                    path.removeFirst();
                    nextCheck--;
                }
            }
            speed = 2;
        } else {
            nearOwner = false;
            speed = 1;
        }
        animDelay--;
        if (animDelay <= 0) {
            animDelay = 5;
            animState++;
            animState %= 3;
        }
        int moveDir = entity.getCurrentLocation().getDir(nextLoc);
        if (moveDir != -1) {
            speed = (nearOwner)?1:3;

            dir = moveDir;
            if (!entity.getCurrentLocation().move(moveDir, speed)) {
                if (owner != null && !nearOwner) {
                    path = as.getPath(entity.getCurrentLocation(), nextLoc, entity.getMap());
                } else {
                    nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
                }
            }
        } else {
            Random rand = new Random();
            if (rand.nextBoolean()) {
                nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
            }
        }

        return true;
    }

    public LivingEntity getOwner() {
        return owner;
    }

    public void setOwner(LivingEntity owner) {
        this.owner = owner;
    }
}
